import { getToken } from "@/utils/storage";

// 登录页面
export const loginPage = "/pages/user/login";
// 首页
export const homePage = "/pages/tabbars/home";

// 页面白名单
const whiteList = [
  "/pages/tabbars/home",
  "/pages/tabbars/guar",
  "/pages/tabbars/comm",
  "/pages/tabbars/mine",

  "/pages/user/forget",
  "/pages/user/login",
  "/pages/user/register"
];

// 检查地址白名单
function checkWhite(url) {
  const path = url.split("?")[0];
  return whiteList.indexOf(path) !== -1;
}

// 页面跳转验证拦截器
const list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
list.forEach(item => {
  uni.addInterceptor(item, {
    invoke(to) {
      // if (getToken()) {
      //   if (to.url === loginPage) {
      //     uni.reLaunch({ url: homePage });
      //   }
      //   return true;
      // } else {
      //   if (checkWhite(to.url)) {
      //     return true;
      //   }
      //   uni.reLaunch({ url: loginPage });
      //   return false;
      // }
      return true;
    },
    fail(err) {
      console.error("页面跳转验证拦截器", err);
    }
  });
});
